/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.system.common.page;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.utils.HttpContext;

import javax.servlet.http.HttpServletRequest;

/**
 * Layui Table默认的分页参数创建
 *
 * @author value
 * @date 2017-04-05 22:25
 */
public class PageFactory {

    /**
     * 获取layui table的分页参数
     *
     * @author value
     * @Date 2019/1/25 22:13
     */
    public static Page<Object> buildPage() {
        HttpServletRequest request = HttpContext.getRequest();
        int limitInt, page;
        // 每页多少条数据
        String limit = request.getParameter("limit");
        if (StrUtil.isBlank(limit)) {
            limitInt = 10;
        } else {
            limitInt = Integer.parseInt(limit);
        }
        String pageStr = request.getParameter("page");
        if (StrUtil.isBlank(pageStr)) {
            page = 1;
        } else {
            page = Integer.parseInt(pageStr);
        }
        return new Page<>(page, limitInt);
    }

    /**
     * 返回分页对象
     *
     * @author value
     * @Date 2019/1/25 22:14
     */
    public static PageInfo buildPageInfo(IPage page) {
        PageInfo result = new PageInfo();
        result.setTotal(page.getTotal());
        result.setRows(page.getRecords());
        return result;
    }
}
